from django.db import models


class YSHAreaCode(models.Model):
    """省市区编码"""

    code = models.CharField(verbose_name='编码', max_length=10, help_text='编码')
    name = models.CharField(max_length=20, verbose_name='名称')
    parent = models.ForeignKey('self', on_delete=models.SET_NULL, related_name='subs', null=True, blank=True, verbose_name='上级行政区划')
    level = models.IntegerField(verbose_name='层级', null=True, blank=True)
    prov_name = models.CharField(verbose_name='省', max_length=64, null=True, blank=True)
    city_name = models.CharField(verbose_name='市', max_length=64, null=True, blank=True)
    district_name = models.CharField(verbose_name='区县', max_length=64, null=True, blank=True)
    prov = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, blank=True, verbose_name='省', related_name="prov_subs")
    city = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, blank=True, verbose_name='市', related_name="city_subs")
    district = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, blank=True, verbose_name='区县', related_name="district_subs")
    prov_code = models.CharField(verbose_name='省', max_length=64, null=True, blank=True)
    city_code = models.CharField(verbose_name='市', max_length=64, null=True, blank=True)
    district_code = models.CharField(verbose_name='区县', max_length=64, null=True, blank=True)

    class Meta:
        db_table = 'tb_ysh_area_code'
        verbose_name = '省市区编码'
        verbose_name_plural = verbose_name


class YSHAliDiscountCoupon(models.Model):
    '''邮生活代金券'''

    TYPE_CHOICES = (
        (1, '微信代金券'),
    )

    USAGE_TYPE_CHOICES = (
        (1, '商品'),
        (2, '活动')
    )

    USAGE_TYPE_ENUM = {
        1: 'MERCHANT',
        2: 'ACTIVITY'
    }

    VERIFY_CHANNEL_CHOICES = (
        (1, '支付宝'),
        (2, '微信/小程序'),
        (3, '通联')
    )

    VERIFY_CHANNEL_ENUM = {
        1: 'ALIPAY',
        2: 'WECHAT',
        3: 'TL',
    }

    ENVIRONMENT_CHOICES = (
        (1, '正式环境'),
        (2, '测试环境')
    )

    alidiscountcoupon = models.OneToOneField('vouchers.AliDiscountCoupon', verbose_name='活动', on_delete=models.CASCADE)
    equityId = models.CharField(verbose_name='卡券id', max_length=50)
    name = models.CharField(verbose_name='卡券名', max_length=50)
    type = models.SmallIntegerField(verbose_name='卡券类型', choices=TYPE_CHOICES, default=1)
    startedAt = models.DateTimeField(verbose_name='卡券有效开始时间')
    endedAt = models.DateTimeField(verbose_name='卡券有效结束时间')
    inventory = models.IntegerField(verbose_name='卡券剩余库存')
    totalInventory = models.IntegerField(verbose_name='卡券总库存')
    couponAmount = models.IntegerField(verbose_name='核销代金券券面额，单位: 分')
    transactionMinimum = models.IntegerField(verbose_name='核销代金券券门槛，单位：分')
    maxCouponsPerUser = models.IntegerField(verbose_name='代金券单个用户可领取数', default=1)
    mchUrl = models.CharField(verbose_name='代金券可用商户展示页面链接', max_length=256)
    sourceDistCode = models.ForeignKey('postallive.YSHAreaCode', verbose_name='卡券来源的行政区划码', on_delete=models.CASCADE)
    showDistCode = models.CharField(verbose_name='展示商品的省份，多个地区则以数组形式存储', max_length=256, null=True, blank=True)
    area = models.CharField(verbose_name='门店归属地区，多个地区则以数组形式存储', max_length=256, null=True, blank=True)
    usageType = models.SmallIntegerField(verbose_name='卡券使用类型', choices=USAGE_TYPE_CHOICES, default=1)
    verifyChannel = models.SmallIntegerField(verbose_name='核销渠道', choices=VERIFY_CHANNEL_CHOICES, default=1)
    serialNo = models.CharField(verbose_name='请求回传单号', max_length=256, null=True, blank=True)
    user = models.ForeignKey('users.User', verbose_name='上传用户', on_delete=models.SET_NULL, null=True, blank=True)
    create_time = models.DateTimeField(verbose_name='操作时间', null=True, blank=True)
    identify = models.CharField(verbose_name='领取标识', max_length=32, unique=True)
    environment = models.SmallIntegerField(verbose_name='环境', choices=ENVIRONMENT_CHOICES, default=1)
    contract = models.ForeignKey('postallive.YSHContract', verbose_name='合同', on_delete=models.SET_NULL, null=True, blank=True)
    costAmount = models.IntegerField(verbose_name='成本面额', null=True, blank=True)

    class Meta:
        db_table = "tb_ysh_alidiscountcoupon"
        verbose_name = '邮生活代金券'
        verbose_name_plural = verbose_name


class YSHContract(models.Model):
    '''邮生活合同'''

    IS_DELETE_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    supplierCode = models.CharField(verbose_name='供应商code', max_length=32)
    supplierName = models.CharField(verbose_name='供应商名称', max_length=128)
    contractCode = models.CharField(verbose_name='合同code', max_length=32)
    contractName = models.CharField(verbose_name='合同名称', max_length=128)
    taxRate = models.DecimalField(verbose_name='税率', max_digits=10, decimal_places=4, default=0)
    is_delete = models.SmallIntegerField(verbose_name='是否删除', choices=IS_DELETE_CHOICES, default=0)

    class Meta:
        db_table = "tb_ysh_contract"
        verbose_name = '邮生活合同'
        verbose_name_plural = verbose_name


class YSPushDocumentQuest(models.Model):
    '''邮生活推送任务'''

    ENVIRONMENT_CHOICES = (
        (1, '正式环境'),
        (2, '测试环境')
    )

    FILE_TYPE_CHOICES = (
        (1, '结算文件'),
        (2, '核销文件')
    )

    push_file = models.ImageField(verbose_name='推送文件')
    original_file = models.ImageField(verbose_name='原文件')
    create_time = models.DateTimeField(verbose_name='推送时间')
    environment = models.SmallIntegerField(verbose_name='环境', choices=ENVIRONMENT_CHOICES, default=1)
    file_type = models.SmallIntegerField(verbose_name='文件类型', choices=FILE_TYPE_CHOICES, default=1)
    result = models.TextField(verbose_name='响应值', null=True, blank=True)

    class Meta:
        db_table = "tb_ysh_push_document_quest"
        verbose_name = '邮生活推送任务'
        verbose_name_plural = verbose_name
